package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicWarehouseMaterialoutDAO;
import com.cssy.xxkj.logic.dao.LogicWarehouseMaterialoutItemDAO;
import com.cssy.xxkj.logic.entity.LogicWarehouseDetailsDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseMaterialoutDO;
import com.cssy.xxkj.logic.entity.LogicWarehouseMaterialoutItemDO;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.cssy.xxkj.logic.service.ILogicWarehouseInventoryService;
import com.cssy.xxkj.logic.service.IMaterialOutService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;
@Service("materialOutServiceImpl")
public class MaterialOutServiceImpl implements IMaterialOutService {
	
	@Resource(name="logicWarehouseMaterialoutDAO")
	private LogicWarehouseMaterialoutDAO logicWarehouseMaterialoutDAO;
	
	@Resource(name="logicWarehouseMaterialoutItemDAO")
	private LogicWarehouseMaterialoutItemDAO logicWarehouseMaterialoutItemDAO;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		List<LogicWarehouseMaterialoutDO> list=logicWarehouseMaterialoutDAO.find(scs);
		SearchResult sr=new SearchResult(scs);
		sr.setResults(list);
		sr.setRecordCount(logicWarehouseMaterialoutDAO.count(scs));
		return sr;
	}
	
	@Override
	public List<LogicWarehouseMaterialoutItemDO> findItemListByMap(Map<String, Object> paramMap)
			throws SyBaseException{
		return logicWarehouseMaterialoutItemDAO.findListByMap(paramMap);
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Integer update(LogicWarehouseMaterialoutDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return logicWarehouseMaterialoutDAO.updateLogicWarehouseMaterialoutDO(t);
	}

	@Override
	public LogicWarehouseMaterialoutDO save(LogicWarehouseMaterialoutDO t,
			User user) throws SyBaseException {
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		t.setBizVersion(1);
		logicWarehouseMaterialoutDAO.insertLogicWarehouseMaterialoutDO(t);
		return t;
	}

	@Override
	public LogicWarehouseMaterialoutDO findListById(String id)
			throws SyBaseException {
		return logicWarehouseMaterialoutDAO.findLogicWarehouseMaterialoutDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return null;
	}

	@Override
	public void outSave(LogicWarehouseMaterialoutDO curAP,
			HttpServletRequest request) throws SyBaseException {
		try{
			String json = request.getParameter("addressInfos");
			List<LogicWarehouseMaterialoutItemDO> axxlist = JSONArray.parseArray(json,LogicWarehouseMaterialoutItemDO.class);
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				if(!curAP.getZd003BillStatus().equals(BillStatusUtil.MATERIALOUT_BILL_STATUE_FOUR)){
					String zd010OtherDate = request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					logicWarehouseMaterialoutItemDAO.deleteByPid(curAP.getId());
					for (LogicWarehouseMaterialoutItemDO lii : axxlist){
						String id = IDCodeTypeUtil.getId();
						lii.setId(id);
						lii.setZd001MaterialOutId(curAP.getId());
						logicWarehouseMaterialoutItemDAO.insertLogicWarehouseMaterialoutItemDO(lii);
					}
				}
				if (curAP.getZd003BillStatus().equals(BillStatusUtil.MATERIALOUT_BILL_STATUE_FOUR)){
					int rt=0;
					rt=this.update(curAP, SessionUtil.getUser(request));
					if(0==rt){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
					LogicWarehouseMaterialoutDO loi = logicWarehouseMaterialoutDAO.findLogicWarehouseMaterialoutDOByPrimaryKey(curAP.getId());
					for (LogicWarehouseMaterialoutItemDO lii : axxlist) {
						/*LogicWarehouseDetailsDO lw = new LogicWarehouseDetailsDO();
						lw.setZd001BillCode(loi.getZd001MaterialCode());
						lw.setZd019Batch(lii.getZd010Batch());
						lw.setZd002BillType(BillTypeConstant.MATERIALOUT);//生产领料
						lw.setZd005MaterialsId(lii.getZd002MaterialsId());
						lw.setZd005MaterialsCode(lii.getZd002MaterialsCode());
						lw.setZd005MaterialsName(lii.getZd002MaterialsName());
						lw.setZd006MaterialsSpec(lii.getZd003MaterialsSpec());
						lw.setZd007MaterialsModel(lii.getZd004MaterialsModel());
						lw.setBizCreatorName(SessionUtil.getUser(request).getName());
						lw.setBizCreatorDate(new Date());
						lw.setZd012Status(lii.getZd008Status());
						lw.setZd004WhPositionId(lii.getZd011WhLocationId());
						lw.setZd004WhPositionCode(lii.getZd012WhLocationCode());
						lw.setZd003WhId(curAP.getZd011WhId());
						lw.setZd003WhName(curAP.getZd011WhName());
						lw.setZd003WhCode(curAP.getZd011WhCode());
						lw.setZd009BasicUnit(lii.getZd006Unit());
						lw.setZd011BasicQuantity(lii.getZd007Quantity().negate());
						logicWarehouseInventoryService.updateWhAffstockQuantity(lw,SessionUtil.getUser(request), false);*/
						
						logicHsUtilService.processMaterailsOutItem(loi, lii, SessionUtil.getUser(request));
					}
				}
			}else{
					curAP.setZd001MaterialCode(syCodeService.getCode(BillTypeConstant.MATERIALOUT,BillTypeConstant.MATERIALOUT,SyCode.DATE_YYMM,5));
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					String zd010OtherDate = request.getParameter("zd010OtherDate");
					curAP.setZd010OtherDate(DateUtils.stringToDate(zd010OtherDate));
					this.save(curAP, SessionUtil.getUser(request));
					for (LogicWarehouseMaterialoutItemDO lii : axxlist){
						lii.setId(IDCodeTypeUtil.getId());
						lii.setZd001MaterialOutId(id);
						logicWarehouseMaterialoutItemDAO.insertLogicWarehouseMaterialoutItemDO(lii);
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
	}

}
